﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 游戏平台原型
{
    public partial class selectroomwindow : UserControl
    {
        private NOPHandler m_showroom;//声明委托变量
        public selectroomwindow(NOPHandler showroom)
        {
            InitializeComponent();
            this.m_showroom= showroom;
            this.lvi_room_roominfo.Columns.Add("房间号", 75, HorizontalAlignment.Left); //一步添加
            this.lvi_room_roominfo.Columns.Add("房间名", 105, HorizontalAlignment.Left); //一步添加
            this.lvi_room_roominfo.Columns.Add("创始人", 105, HorizontalAlignment.Left); //一步添加
            this.lvi_room_roominfo.Columns.Add("画板种类", 105, HorizontalAlignment.Left); //一步添加
            this.lvi_room_roominfo.Columns.Add("当前人数", 60, HorizontalAlignment.Left); //一步添加
            this.lvi_room_roominfo.Columns.Add("最大人数", 60, HorizontalAlignment.Left); //一步添加
            this.lvi_room_roominfo.Columns.Add("当前画板数", 60, HorizontalAlignment.Left); //一步添加
            this.lvi_room_roominfo.Columns.Add("最大画板数", 60, HorizontalAlignment.Left); //一步添加
        }

        private void selectroomwindow_Load(object sender, EventArgs e)
        {
            tb_selectroom_welcome.Text = "欢迎你，" + User.user.currentusername + "，您的id为" + User.user.currentuserno;
        }

        private void btn_room_addroom_Click(object sender, EventArgs e)
        {
            Form_AddRoom form = new Form_AddRoom();
            form.ShowDialog();
            updateroominfo();
        }

        private void btn_room_selectroom_Click(object sender, EventArgs e)
        {
            if (this.m_showroom != null)
            {
                User.user.enterroom(User.user.currentuserno, long.Parse(lvi_room_roominfo.FocusedItem.SubItems[0].Text));
                this.m_showroom();
            }

        }
        private void updateroominfo()
        {
            lvi_room_roominfo.Items.Clear();
            this.lvi_room_roominfo.BeginUpdate();   //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度
            Room p = Lobby.Lobby1.head;
            while (p != null)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = p.roomid.ToString();
                lvi.SubItems.Add(p.roomname);
                lvi.SubItems.Add(User.no2name(p.ownerid));
                lvi.SubItems.Add("");
                lvi.SubItems.Add(p.onlinelist.ListLength().ToString());
                lvi.SubItems.Add(p.maxperson.ToString());
                lvi.SubItems.Add(p.PanelCount().ToString());
                lvi.SubItems.Add(p.maxpanel.ToString());
                this.lvi_room_roominfo.Items.Add(lvi);
                p = p.next;
            }
            this.lvi_room_roominfo.EndUpdate();  //结束数据处理，UI界面一次性绘制。
        }
    }
}
